﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DigitalAnts.MasterPage;
using System.Drawing;
using System.Data;
using BAL.Entities;
using BAL.Components;

namespace DigitalAnts.User
{
    public partial class ProductListByUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ((UserLayout)Page.Master).ProductList = false;
                ((UserLayout)Page.Master).ProductListHyperLinkColor = Color.Red;
                ((UserLayout)Page.Master).UnabelProductListHyplerLinkUnderline = false;
            }
            doGetProductListByUser();
        }

        public void doGetProductListByUser()
        {
            clsUser obj = new clsUser();
            obj.UserID = Convert.ToInt32(Session["UserID"]);
            DataTable dt = clsProductBAL.doProductListByUser(obj);
            gvProductList.DataSource = dt;
            gvProductList.DataBind();
        }

        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }

        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvProductList.PageIndex = e.NewPageIndex;
            gvProductList.DataBind();
        }

        protected void gv_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = gvProductList.DataSource as DataTable;

            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvProductList.DataSource = dataView;
                gvProductList.DataBind();
            }
        }

        protected void gvProductList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productID = Int32.Parse(gvProductList.DataKeys[e.RowIndex].Value.ToString());
            clsProduct obj = new clsProduct();
            obj.ProductID = productID;
            if (clsProductBAL.doDeleteProduct(obj) == true)
            {
                doGetProductListByUser();
            }
        }


    }
}